{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6a3899a0-a3a6-4924-9150-0dc71c59fab9",
   "metadata": {},
   "source": [
    "# 一、Numpy数据的增删改查\n",
    "## 1.1、数据的增加和删除\n",
    "### 1.1.1、数据追加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "792af217-cc08-42a9-8f21-ba9fda45f57d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "arr = np.array([1,2,3,4,5])\n",
    "#np.append(arr,[xx,xx])在最后追加元素，之后会产生一个新的数组\n",
    "arr2 = np.append(arr,[88,99])\n",
    "print(arr)\n",
    "print(arr2)\n",
    "arr3 = np.array([[1,2],[3,4]])\n",
    "#numpy会自动降维，如果希望不降维，需要加第三个参数axis,有两个值,0表示纵向处理，1表示横向处理,axis默认是None\n",
    "#插入的时候维度要相同，位置能够对齐\n",
    "arr4 = np.append(arr3,[[5,6]],axis=0)\n",
    "print(arr4)\n",
    "#插入列\n",
    "arr5 = np.append(arr3,[[5],[6]],axis=1)\n",
    "print(arr5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cb8ae60-8921-497c-8f46-bed5151ef698",
   "metadata": {},
   "source": [
    "axis 表示“轴”的方向”，也就是沿着哪一个维度进行操作。\n",
    "简单理解：\n",
    "- axis=0 → 沿着行方向（纵向）操作\n",
    "- axis=1 → 沿着列方向（横向）操作\n",
    "\n",
    "当不指定 axis 参数时，NumPy 会先把数组“拉平”（flatten）成一维，再执行操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f987c77d-7078-4af3-a0a9-621a60a7b18b",
   "metadata": {},
   "source": [
    "### 1.1.2、数据的插入\n",
    "append只能在最后追加，而insert可以指定位置插入数据,insert和append类似，都是会产生一个新的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a8c7489-3c7f-4e7b-be04-688dff9d93c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([1,2,3,4,5])\n",
    "#在第二个位置插入99\n",
    "arr2 = np.insert(arr1,2,99)\n",
    "print(arr2)\n",
    "#在0和3的位置插入88和99\n",
    "arr3 = np.insert(arr1,[0,3],[88,99])\n",
    "print(arr3)\n",
    "#在0的位置插入两个元素\n",
    "arr4 = np.insert(arr1,0,[88,999])\n",
    "print(arr4)\n",
    "print(\"*\"*100)\n",
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print(arr)\n",
    "#在第一行插入[111,222,333]\n",
    "arr2 = np.insert(arr,1,[111,222,333],axis=0)\n",
    "print(arr2)\n",
    "#在第一列插入元素,insert和append在这里是不一样的\n",
    "arr3 = np.insert(arr,1,[66,88],axis=1)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c01751da-869d-44de-81a6-807aa2b646a3",
   "metadata": {},
   "source": [
    "### 1.1.3、数据的删除\n",
    "数据的删除使用np.delete(arr,下标),和append都是会产生新的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e295ace2-aaf1-4d4a-85c0-68c64c9f7a61",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([1,2,3,4,5])\n",
    "#删除下标为1和3的元素\n",
    "arr2 = np.delete(arr,[1,3])\n",
    "print(arr2)\n",
    "arr = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(arr)\n",
    "print(\"*\"*100)\n",
    "#删除0和2两行\n",
    "arr2 = np.delete(arr,[0,2],axis=0)\n",
    "print(arr2)\n",
    "#删除第0,1列\n",
    "arr3 = np.delete(arr,[0,1],axis=1)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8e8bb88-4e60-48a9-b022-616846d53336",
   "metadata": {},
   "source": [
    "### 1.1.4、数据的合并\n",
    "np.concatenate()进行合并，需要注意，这个合并必须对齐，维数相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b87e8d5e-a9ac-46c3-932c-220db3df4217",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#创建一个3*3的数组\n",
    "a = np.arange(1,10).reshape(3,-1)\n",
    "#创建一个一行的二维数组\n",
    "b = np.array([[1,2,3]])\n",
    "print(a,a.ndim)\n",
    "print(b,b.ndim)\n",
    "#从行上面合并a和b\n",
    "arr1 = np.concatenate((a,b),axis=0)\n",
    "print(\"*\"*100)\n",
    "print(arr1)\n",
    "#如果希望从列合并，也必须完全对齐，和append一样，和insert不一样\n",
    "c = np.array([[22],[33],[44]])\n",
    "arr2 = np.concatenate((a,c),axis=1)\n",
    "print(arr2)\n",
    "#由于合并的功能比较常用，numpy提供了两个方法:vstack进行纵向合并hstack进行横向合并(其实就是它帮你实现了axis=0)\n",
    "print(np.vstack((b,a)))\n",
    "print(np.hstack((c,a)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2001822a-30a6-4e2d-a451-5dcf9365a02b",
   "metadata": {},
   "source": [
    "### 1.1.5、练习\n",
    "#### **追加和插入**\n",
    " **基础题：**\n",
    "  - 创建数组 `arr = np.array([1,2,3,4,5])`，在末尾追加 `[6,7,8]`。  \n",
    "  - 将上题中的数组改成二维形状 `(2,4)`，在行方向追加一行 `[9,10,11,12]`。  \n",
    "  - 在一维数组 `[10,20,30,40]` 的索引 2 位置插入元素 `99`。  \n",
    "  - 创建二维数组  \n",
    "    ```\n",
    "    [[1,2,3],\n",
    "     [4,5,6],\n",
    "     [7,8,9]]\n",
    "    ```\n",
    "    在第二行（索引 1）后插入新列 `[11,22,33]`。  \n",
    "\n",
    "***\n",
    "\n",
    "**进阶题：**\n",
    "  - 创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，在列方向插入一列全 0。  \n",
    "  - 在行方向追加一行 `[100,200,300,400]`。  \n",
    "  - 先把数组展平，再在索引 5 的位置插入 `999`。  \n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a0da958-5a59-49e8-afcd-f9a51b87f55e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#创建数组 arr = np.array([1,2,3,4,5])，在末尾追加 [6,7,8]。\n",
    "arr = np.array([1,2,3,4,5])\n",
    "arr2 = np.append(arr,[6,7,8])\n",
    "print(arr2)\n",
    "print('*'*100)\n",
    "#将上题中的数组改成二维形状 (2,4)，在行方向追加一行 [9,10,11,12]。\n",
    "arr1 = arr2.reshape(2,4)\n",
    "print(arr1)\n",
    "arr3 = np.append(arr1,[[9,10,11,12]],axis=0)\n",
    "print(arr3)\n",
    "#在一维数组 [10,20,30,40] 的索引 2 位置插入元素 99。\n",
    "print('*'*100)\n",
    "arr = np.array([10,20,30,40])\n",
    "arr2 = np.insert(arr,2,99)\n",
    "print(arr2)\n",
    "#创建二维数组\n",
    "'''[[1,2,3],\n",
    " [4,5,6],\n",
    " [7,8,9]]\n",
    "在第二行（索引 1）后插入新列 [11,22,33]。\n",
    "'''\n",
    "print('*'*100)\n",
    "arr = np.array([\n",
    "    [1,2,3],\n",
    "    [4,5,6],\n",
    "    [7,8,9]\n",
    "])\n",
    "#在第二行\n",
    "arr2 = np.insert(arr,1,[[11,22,33]],axis=0)\n",
    "print(arr2)\n",
    "arr3 = np.insert(arr,1,[11,22,33],axis=1)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9ef22d1-d8b7-4273-a311-c9a412055fa5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#创建二维数组 arr = np.arange(1,13).reshape(3,4)，在列方向插入一列全 0。\n",
    "arr = np.arange(1,21).reshape(4,5)\n",
    "print(arr.shape[0])\n",
    "arr2 = np.insert(arr,0,np.zeros(arr.shape[0]),axis=1)\n",
    "print(arr2)\n",
    "print('*'*100)\n",
    "#在行方向追加一行 [100,200,300,400,500]。\n",
    "arr3 = np.insert(arr,0,[100,200,300,400,500],axis=0)\n",
    "print(arr3)\n",
    "#先把数组展平，再在索引 5 的位置插入 999。\n",
    "arr4 = np.insert(arr,5,999)\n",
    "print(arr4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f285640a-3cfb-4d29-952a-a44910bb6927",
   "metadata": {},
   "source": [
    "#### **数据删除**\n",
    "**基础题：**\n",
    "  - 创建数组 `[10,20,30,40,50]`，删除索引 2 的元素。  \n",
    "  - 创建二维数组  \n",
    "    ```\n",
    "    [[1,2,3],\n",
    "     [4,5,6],\n",
    "     [7,8,9]]\n",
    "    ```\n",
    "    删除第 1 行（索引 0）。  \n",
    "  - 删除上面数组的第 2 列（索引 1）。  \n",
    "  - 删除一维数组 `[1,2,3,4,5,6,7,8]` 中所有偶数（提示：用布尔条件取索引）。  \n",
    "\n",
    "***\n",
    "\n",
    "**进阶题：**\n",
    "  - 创建 `arr = np.arange(1,13).reshape(3,4)`，删除所有大于 8 的元素。  \n",
    "  - 删除第一行和最后一列。  \n",
    "  - 删除第 1、3、5 索引位置的元素（可使用列表）。  \n",
    "  - 删除二维数组的所有奇数列。  \n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82926053-010a-4b10-ace7-5b7b3aa86592",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#创建数组 [10,20,30,40,50]，删除索引 2 的元素。\n",
    "arr = np.array([10,20,30,40,50])\n",
    "arr2 = np.delete(arr,2)\n",
    "print(arr2)\n",
    "print('*'*100)\n",
    "#创建二维数组\n",
    "#[[1,2,3],\n",
    "# [4,5,6],\n",
    "# [7,8,9]]\n",
    "#删除第 1 行（索引 0）。\n",
    "#删除上面数组的第 2 列（索引 1）。\n",
    "arr = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "arr2 = np.delete(arr,0,axis=0)\n",
    "print(arr2)\n",
    "arr3 = np.delete(arr,1,axis=1)\n",
    "print(arr3)\n",
    "print('*'*100)\n",
    "#删除一维数组 [1,2,3,4,5,6,7,8] 中所有偶数（提示：用布尔条件取索引）。\n",
    "arr = np.arange(1,8)\n",
    "print(arr)\n",
    "arr2 = np.delete(arr,arr%2==0)\n",
    "print(arr2)\n",
    "print('*'*100)\n",
    "#创建 arr = np.arange(1,13).reshape(3,4)，删除所有大于 8 的元素。\n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "arr2 = arr[arr<=8]\n",
    "print(arr2)\n",
    "#删除第一行和最后一列。\n",
    "arr3 = np.delete(arr,0,axis=0)\n",
    "print(arr3)\n",
    "arr4 = np.delete(arr,-1,axis=1)\n",
    "print(arr4)\n",
    "#删除第 1、3、5 索引位置的元素（可使用列表）。\n",
    "arr5 = np.delete(arr,[1,3,5])\n",
    "print(arr5)\n",
    "#删除二维数组的所有奇数列。\n",
    "arr6 = arr[:,::2]\n",
    "print(arr6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b92e3d0c-54cd-4204-8d35-73ee00df014f",
   "metadata": {},
   "source": [
    "#### **数据合并**\n",
    "**基础题：**\n",
    "  - 创建两个数组  \n",
    "    ```\n",
    "    a = [[1,2],\n",
    "         [3,4]]\n",
    "    b = [[5,6],\n",
    "         [7,8]]\n",
    "    ```\n",
    "    分别沿 axis=0 和 axis=1 拼接。  \n",
    "  - 将三个一维数组 `[1,2]`、`[3,4]`、`[5,6]` 合并成一个。  \n",
    "  - 将两个 `(2,3)` 数组纵向拼接成 `(4,3)`。  \n",
    "  - 将两个 `(3,2)` 数组横向拼接成 `(3,4)`。  \n",
    "\n",
    "***\n",
    "\n",
    "**进阶题：**\n",
    "  - 创建 `a = np.arange(6).reshape(2,3)`，`b = np.arange(6,12).reshape(2,3)`，按行拼接。  \n",
    "  - 创建 `a = np.arange(9).reshape(3,3)`，将它与自身横向拼接（重复一次）。  \n",
    "  - 尝试将形状不同的数组合并，观察报错信息并解释原因。  \n",
    "  - 将 `[1,2,3]`、`[4,5,6]`、`[7,8,9]` 这三个一维数组合并为一个 3×3 二维数组。  \n",
    "\n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd52f824-8cbc-41a2-8c31-454b0629297b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "'''\n",
    "创建两个数组\n",
    "a = [[1,2],\n",
    "     [3,4]]\n",
    "b = [[5,6],\n",
    "     [7,8]]\n",
    "'''\n",
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "#分别沿 axis=0 和 axis=1 拼接。\n",
    "arr1 = np.concatenate([a,b],axis=0)\n",
    "print(arr1)\n",
    "arr2 = np.concatenate((a,b),axis=1)\n",
    "print(arr2)\n",
    "#将三个一维数组 [1,2]、[3,4]、[5,6] 合并成一个。\n",
    "a = np.array([1,2])\n",
    "b = np.array([3,4])\n",
    "c = np.array([5,6])\n",
    "print(np.vstack((a,b,c)))\n",
    "arr1 = np.concatenate((a,b,c),axis=0)\n",
    "print(arr1)\n",
    "arr2 = np.concatenate(([a],[b],[c]),axis=0)\n",
    "print(arr2)\n",
    "#将两个 (2,3) 数组纵向拼接成 (4,3)。\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "b = np.array([[1,1,1],[2,2,2]])\n",
    "arr = np.vstack((a,b))\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "#将两个 (3,2) 数组横向拼接成 (3,4)。\n",
    "a = np.array([[1,2],[3,4],[5,6]])\n",
    "b = np.array([[1,1],[2,2],[3,3]])\n",
    "arr = np.hstack((a,b))\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c11d43ff-fe88-44bd-a395-00d1ee1f5ea6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#创建 a = np.arange(6).reshape(2,3)，b = np.arange(6,12).reshape(2,3)，按行拼接。\n",
    "a = np.arange(6).reshape(2,3)\n",
    "b = np.arange(6,12).reshape(2,3)\n",
    "arr1 = np.concatenate((a,b),axis=0)\n",
    "arr2 = np.vstack((a,b))\n",
    "print(arr1)\n",
    "print('*'*100)\n",
    "print(arr2)\n",
    "print('*'*100)\n",
    "#创建 a = np.arange(9).reshape(3,3)，将它与自身横向拼接（重复一次）。\n",
    "a = np.arange(9).reshape(3,3)\n",
    "arr = np.hstack((a,a))\n",
    "print(arr)\n",
    "#尝试将形状不同的数组合并，观察报错信息并解释原因。\n",
    "#将 [1,2,3]、[4,5,6]、[7,8,9] 这三个一维数组合并为一个 3×3 二维数组。\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([4,5,6])\n",
    "c = np.array([7,8,9])\n",
    "arr = np.vstack((a,b,c))\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f07ab04-8d8c-4312-98cc-ba6846737dd0",
   "metadata": {},
   "source": [
    "## 1.2、numpy数据的修改\n",
    "### 1.2.1、直接修改和切片修改\n",
    "多数修改操作都是在原有的数组上面处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da28ac60-7f07-486c-9920-5dd60ccfb2a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "arr = np.array([1,2,3,4,5])\n",
    "arr[0] = 10\n",
    "print(arr)\n",
    "arr[1:4] = [5,9,8]\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "016106ed-15d9-4781-9455-dd9cd56759c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([\n",
    "    [1,2,3],\n",
    "    [4,5,6],\n",
    "    [7,8,9]\n",
    "])\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "#直接修改,将第一行第二列的元素修改位12\n",
    "arr[1,2] = 12\n",
    "print(arr)\n",
    "#修改第一行为999\n",
    "arr[1] = [9,9,9]\n",
    "print(arr)\n",
    "arr[:,1] = [8,8,8]\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f74df1fd-75d5-4f9a-885a-14d059472fb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([1,2,3,4,5])\n",
    "arr+=10\n",
    "print(arr)\n",
    "arr[::2]*=10\n",
    "print(arr)\n",
    "arr1 = np.array([3,4,5,6,7])\n",
    "print(arr*arr1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e5c8a44-742a-4b56-b4eb-857ff210b389",
   "metadata": {},
   "source": [
    "### 1.2.2、花式索引进行修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdfa1931-1f0b-498b-92e5-1aa086575df4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "arr = np.array([10,20,30,40,50,60])\n",
    "print(arr)\n",
    "arr[[1,3,4]] = [999,999,999]\n",
    "print(arr)\n",
    "arr = np.array([\n",
    "    [1,2,3],\n",
    "    [4,5,6],\n",
    "    [8,9,10]\n",
    "])\n",
    "#手拉手，修改的是(1,2),(2,1)\n",
    "arr[[1,2],[2,1]] = [888,999]\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "#把0和2两行中的1,2两列的数据修改为666\n",
    "arr[[0,2],1:3] = 666\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "arr = np.arange(1,17).reshape(4,4)\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "#此处就是一个矩阵修改\n",
    "arr[np.ix_([0,2],[0,2])] = 0\n",
    "print(arr)\n",
    "arr = np.random.randint(50,170,(10,15))\n",
    "print(arr)\n",
    "#把所有大于150分的都修改为-1\n",
    "arr[arr>150] = -1\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd302299-cf5a-4522-9ecf-e62b59acbf61",
   "metadata": {},
   "source": [
    "### 1.2.3、常用的修改方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3d16fb5-ea21-4df7-8625-2ed28a04c9ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "arr = np.array([1,2,3,4,5,6,7])\n",
    "np.put(arr,[1,4],[88,99])\n",
    "print(arr)\n",
    "arr = np.array([[1,2,3,4],[5,6,7,8]])\n",
    "print(arr)\n",
    "np.put(arr,[0,1,5],[666,777,888])\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e03a15f4-6c14-4c50-98a2-b8f2d6da6b11",
   "metadata": {},
   "outputs": [],
   "source": [
    "#where方法的修改，np.where(条件，满足时修改的值，不满组修改的值)\n",
    "arr = np.random.randint(10,100,(10,10))\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "#小于20的修改为0，其他的不变\n",
    "arr1 = np.where(arr<20,0,arr)\n",
    "print(arr1)\n",
    "arr2 = np.where(arr<20,0,arr+100)\n",
    "print(arr2)\n",
    "arr3 = np.where(arr<30,arr.mean(),arr)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "023e35a2-66c4-4588-ad51-257bbf7b4913",
   "metadata": {},
   "source": [
    "## 1.3、NumPy 综合练习题\n",
    "\n",
    "> 适用范围：索引 / 切片 / 添加 / 删除 / 拼接 / 修改 / 花式索引 / 布尔索引 / where / put  \n",
    "> 使用前请导入：`import numpy as np`\n",
    "#### 练习1\n",
    "***\n",
    "- 1、创建一个长度为10的一维全为0的ndarray对象，然后让第5个元素等于1\n",
    "  \n",
    "- 2、创建一个元素为从10到49的ndarray对象，间隔是1\n",
    "\n",
    "- 3、将第2题的所有元素位置反转\n",
    "\n",
    "- 4、使用np.random.random创建一个10\\*10的ndarray对象，并打印出最大最小元素(arr.max(),arr.min()函数)\n",
    "\n",
    "- 5、创建一个10\\*10的ndarray对象，且矩阵边界全为1，里面全为0\n",
    "\n",
    "- 6、创建一个每一行都是从0到4的5\\*5矩阵\n",
    "\n",
    "- 7、创建一个范围在(0,1)之间的长度为12的等差数列。\n",
    "\n",
    "- 8、创建一个长度为10的正太分布数组np.random.randn并排序（np.sort()）\n",
    "\n",
    "- 9、创建一个长度为10的随机数组并将最大值替换为-100\n",
    "\n",
    "- 10、如何根据第3列大小顺序来对一个5\\*5矩阵排序？\n",
    "\n",
    "#### 练习2\n",
    "***\n",
    "\n",
    "- （1）创建数组 `arr = np.arange(10, 20)`，取出第 3~7 个元素；再取出所有奇数索引位置的元素；并将最后三个元素改为 `0`。  \n",
    "\n",
    "- （2）创建二维数组 `arr2 = np.arange(1,13).reshape(3,4)`，取出第二行和第三列；将第二行的所有元素改为 `99`；再将所有大于 `8` 的元素改为 `0`。  \n",
    "\n",
    "- （3）创建数组 `arr = np.array([1,2,3,4,5])`，在末尾追加 `[6,7,8]`；然后在索引 `2` 位置插入 `99`；在索引 `3` 位置插入 `[100,101]`。  \n",
    "\n",
    "- （4）创建二维数组 `arr = np.arange(1,10).reshape(3,3)`，在第 `1` 列插入新列 `[100,200,300]`（`axis=1`）。  \n",
    "\n",
    "- （5）创建数组 `arr = np.arange(1,11)`，删除索引 `[1,3,5]` 的元素；再删除当前数组中的所有偶数元素（使用布尔索引）。  \n",
    "\n",
    "- （6）创建二维数组 `arr = np.arange(12).reshape(3,4)`，删除第 `0` 行与第 `2` 列（分别指定 `axis=0/1`）。  \n",
    "\n",
    "- （7）给定三个数组  \n",
    "    a = `np.array([1,2])`，b = `np.array([3,4])`，c = `np.array([5,6])`：  \n",
    "    ① 使用 `np.concatenate` 合并为一维；  \n",
    "    ② 使用 `np.stack` 合并为二维（3×2）；  \n",
    "    ③ 分别用 `np.vstack` 与 `np.hstack` 展示纵向与横向拼接。  \n",
    "\n",
    "- （8）创建数组 `arr = np.array([10,20,30,40,50,60])`，用花式索引将索引 `[1,3,5]` 的元素同时改为 `-1`。  \n",
    "\n",
    "- （9）创建数组 `arr = np.array([10,20,30,40,50])`，将所有 `≥ 40` 的元素改为 `0`。  \n",
    "\n",
    "- （10）创建数组 `arr = np.arange(1,11)`，使用 `np.where` 将偶数改为 `0`，奇数保持原值。  \n",
    "\n",
    "- （11）创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，使用 `np.put` 将线性位置 `0、5、10` 的元素改为 `99`（按一维顺序）。  \n",
    "\n",
    "- （12）创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，将第一行所有元素加 `10`；再将最后一列全部改为 `0`。  \n",
    "\n",
    "- （13）创建数组 `arr = np.array([5,10,15,20,25,30])`，将 `> 20` 的元素改为 `-1`；将等于 `10` 的元素改为 `0`。  \n",
    "\n",
    "- （14）创建二维数组  \n",
    "      arr =  \n",
    "          [ [ 5,  2,  8,  1],  \n",
    "            [ 7,  9,  3,  4],  \n",
    "            [ 6,  0, 11, 12] ]  \n",
    "    依次完成：删除第 `2` 行；在末尾添加一行 `[100,101,102,103]`；将第 `0` 行的偶数改为 `0`；将第 `1` 列复制到末列（原列保留）；使用 `np.where` 将所有 `> 50` 的值改为 `-1`。  \n",
    "\n",
    "- （15）创建数组 `arr = np.arange(1,21).reshape(4,5)`，依次完成：删除第 `1` 行；在第 `2` 列插入 `[99,99,99]`；将所有 `> 10` 的元素改为 `0`；将第 `0` 行的奇数改为 `-1`；使用 `np.put` 将线性位置 `0、5、10` 的值设为 `888`；输出最终结果。  \n",
    "\n",
    "***\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c46b6183-c2a1-4e64-b735-fb572fa29209",
   "metadata": {},
   "source": [
    "### 练习一答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "31cae1f8-052a-45cc-bffc-c00b79d569af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n",
      "[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33\n",
      " 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]\n",
      "[49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26\n",
      " 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10]\n",
      "****************************************************************************************************\n",
      "[[32 58 37 28 19 94 78 23 24 95]\n",
      " [12 29 75 89 10 16 19 81 72 89]\n",
      " [12 18 47  8 76 29 34 85 97 89]\n",
      " [45  6  5 72 89 89 51 55 35 16]\n",
      " [78 89 16  7 86 23 12 13 93 97]\n",
      " [63 58 80 43 58 98 51 46 41 90]\n",
      " [74 38  1 19 24  4 30 17 85 83]\n",
      " [15 52 80 18 51 54 26 49 18 33]\n",
      " [82 81 42 91 13 31 82 18 17  1]\n",
      " [32 74 65 39 23 97 67 68 63 96]]\n",
      "****************************************************************************************************\n",
      "1 98\n",
      "[12  6  1  7 10  4 12 13 17  1]\n",
      "[82 89 80 91 89 98 82 85 97 97]\n",
      "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
      " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
      " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
      " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
      " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
      " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
      " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
      " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n",
      " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n",
      "[[0. 1. 2. 3. 4.]\n",
      " [0. 1. 2. 3. 4.]\n",
      " [0. 1. 2. 3. 4.]\n",
      " [0. 1. 2. 3. 4.]\n",
      " [0. 1. 2. 3. 4.]]\n",
      "[0.         0.09090909 0.18181818 0.27272727 0.36363636 0.45454545\n",
      " 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1.        ]\n",
      "[-0.99504103  2.62115252 -0.34088642 -0.16209416  0.43295951 -0.00449346\n",
      " -1.86472139 -0.60772586  2.61912518  0.66359295]\n",
      "****************************************************************************************************\n",
      "[-1.86472139 -0.99504103 -0.60772586 -0.34088642 -0.16209416 -0.00449346\n",
      "  0.43295951  0.66359295  2.61912518  2.62115252]\n",
      "[[74 41 44 91 72]\n",
      " [ 9 86 73 29 31]\n",
      " [90 26 79 82 86]\n",
      " [63 14 42 34  5]\n",
      " [88 95 29 40 92]]\n",
      "****************************************************************************************************\n",
      "[[  74   41   44   91   72]\n",
      " [   9   86   73   29   31]\n",
      " [  90   26   79   82   86]\n",
      " [  63   14   42   34    5]\n",
      " [  88 -100   29   40   92]]\n",
      "[[5 1 2 1 2]\n",
      " [9 1 9 7 5]\n",
      " [4 1 5 7 9]\n",
      " [2 9 5 2 4]\n",
      " [7 6 4 7 2]]\n",
      "****************************************************************************************************\n",
      "[1 0 2 3 4]\n",
      "[[5 1 1 2 2]\n",
      " [9 7 1 9 5]\n",
      " [4 7 1 5 9]\n",
      " [2 2 9 5 4]\n",
      " [7 7 6 4 2]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#1、创建一个长度为10的一维全为0的ndarray对象，然后让第5个元素等于1\n",
    "arr = np.zeros(10)\n",
    "arr[4] = 1\n",
    "print(arr)\n",
    "#2、创建一个元素为从10到49的ndarray对象，间隔是1\n",
    "arr = np.arange(10,50)\n",
    "print(arr)\n",
    "#3、将第2题的所有元素位置反转\n",
    "arr2 = arr[::-1]\n",
    "print(arr2)\n",
    "print('*'*100)\n",
    "#4、使用np.random.random创建一个10*10的ndarray对象，并打印出最大最小元素(arr.max(),arr.min()函数),再打印每一列的最大和最小\n",
    "arr = np.random.randint(1,100,(10,10))\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "print(arr.min(),arr.max())\n",
    "#每一列的最小值\n",
    "print(arr.min(axis=0))\n",
    "print(arr.max(axis=0))\n",
    "#5、创建一个10*10的ndarray对象，且矩阵边界全为1，里面全为0\n",
    "arr = np.zeros((10,10))\n",
    "arr[[0,-1]] = 1\n",
    "arr[:,[0,-1]] = 1\n",
    "print(arr)\n",
    "#6、创建一个每一行都是从0到4的5*5矩阵\n",
    "arr = np.zeros((5,5))\n",
    "arr[0:5] = np.arange(0,5)\n",
    "print(arr)\n",
    "#7、创建一个范围在(0,1)之间的长度为12的等差数列。\n",
    "arr = np.linspace(0,1,12)\n",
    "print(arr)\n",
    "#8、创建一个长度为10的正太分布数组np.random.randn并排序（np.sort()）\n",
    "arr = np.random.randn(10)\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "arr1 = np.sort(arr)\n",
    "print(arr1)\n",
    "#9、创建一个长度为10的随机数组并将最大值替换为-100\n",
    "arr = np.random.randint(1,100,(5,5))\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "arr[arr==arr.max()] = -100\n",
    "print(arr)\n",
    "#10、如何根据第3列大小顺序来对一个5*5矩阵排序？argsort\n",
    "np.random.seed(10)\n",
    "arr = np.random.randint(1,10,(5,5))\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "# print(np.argsort(arr[2]))\n",
    "idx = np.argsort(arr[2])\n",
    "print(idx)\n",
    "print(arr[:,[0,3,1,2,4]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5a9a198-a634-4e13-92ba-3f6aaa254724",
   "metadata": {},
   "source": [
    "### 练习二答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "7046e90e-c90f-4685-bdfc-c975d4af256b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[13 14 15 16 17]\n",
      "[11 13 15 17 19]\n",
      "[10 11 12 13 14 15 16  0  0  0]\n",
      "****************************************************************************************************\n",
      "[5 6 7 8]\n",
      "[[ 3]\n",
      " [ 7]\n",
      " [11]]\n",
      "[[ 1  2  3  4]\n",
      " [99 99 99 99]\n",
      " [ 9 10 11 12]]\n",
      "[[1 2 3 4]\n",
      " [0 0 0 0]\n",
      " [0 0 0 0]]\n",
      "[  1   2  99 100 101   3   4   5   6   7   8]\n",
      "[[  1 100   2   3]\n",
      " [  4 200   5   6]\n",
      " [  7 300   8   9]]\n",
      "****************************************************************************************************\n",
      "[ 1  3  5  7  8  9 10]\n",
      "[1 3 5 7 9]\n",
      "****************************************************************************************************\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "****************************************************************************************************\n",
      "[[ 4  5  7]\n",
      " [ 8  9 11]]\n",
      "****************************************************************************************************\n",
      "[1 2 3 4 5 6]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 3 5]\n",
      " [2 4 6]]\n",
      "[10 -1 30 -1 50 -1]\n",
      "****************************************************************************************************\n",
      "[10 20 30  0  0]\n",
      "****************************************************************************************************\n",
      "[1 0 3 0 5 0 7 0 9 0]\n",
      "****************************************************************************************************\n",
      "[[99  2  3  4]\n",
      " [ 5 99  7  8]\n",
      " [ 9 10 99 12]]\n",
      "****************************************************************************************************\n",
      "[[11 12 13 14]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "****************************************************************************************************\n",
      "[[11 12 13  0]\n",
      " [ 5  6  7  0]\n",
      " [ 9 10 11  0]]\n",
      "****************************************************************************************************\n",
      "[ 5  0 15 20 -1 -1]\n",
      "****************************************************************************************************\n",
      "[[ 5  2  8  1]\n",
      " [ 6  0 11 12]]\n",
      "[[  5   2   8   1]\n",
      " [  6   0  11  12]\n",
      " [100 101 102 103]]\n",
      "[[  5   0   0   1]\n",
      " [  6   0  11  12]\n",
      " [100 101 102 103]]\n",
      "****************************************************************************************************\n",
      "[[  5   0   0   1   0]\n",
      " [  6   0  11  12   0]\n",
      " [100 101 102 103 101]]\n",
      "****************************************************************************************************\n",
      "[[ 5  0  0  1  0]\n",
      " [ 6  0 11 12  0]\n",
      " [-1 -1 -1 -1 -1]]\n",
      "****************************************************************************************************\n",
      "[[ 1  2  3  4  5]\n",
      " [11 12 13 14 15]\n",
      " [16 17 18 19 20]]\n",
      "[[ 1  2 99  3  4  5]\n",
      " [11 12 99 13 14 15]\n",
      " [16 17 99 18 19 20]]\n",
      "[[1 2 0 3 4 5]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]]\n",
      "[[-1  2  0 -1  4 -1]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]]\n",
      "[[888   2   0  -1   4 888]\n",
      " [  0   0   0   0 888   0]\n",
      " [  0   0   0   0   0   0]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#（1）创建数组 `arr = np.arange(10, 20)`，取出第 3~7 个元素；再取出所有奇数索引位置的元素；并将最后三个元素改为 `0`。  \n",
    "arr = np.arange(10, 20)\n",
    "print(arr[3:8])\n",
    "print(arr[1::2])\n",
    "arr[-3::] = 0\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "#（2）创建二维数组 `arr2 = np.arange(1,13).reshape(3,4)`，取出第二行和第三列；将第二行的所有元素改为 `99`；再将所有大于 `8` 的元素改为 `0`。  \n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "print(arr[1])\n",
    "print(arr[:,2:3])\n",
    "arr[1] = 99\n",
    "print(arr)\n",
    "arr[arr>8]=0\n",
    "print(arr)\n",
    "\n",
    "#（3）创建数组 `arr = np.array([1,2,3,4,5])`，在末尾追加 `[6,7,8]`；然后在索引 `2` 位置插入 `99`；在索引 `3` 位置插入 `[100,101]`。  \n",
    "arr = np.array([1,2,3,4,5])\n",
    "arr2 = np.append(arr,[6,7,8])\n",
    "arr3 = np.insert(arr2,2,99)\n",
    "arr4 = np.insert(arr3,3,[100,101])\n",
    "print(arr4)\n",
    "# （4）创建二维数组 `arr = np.arange(1,10).reshape(3,3)`，在第 `1` 列插入新列 `[100,200,300]`（`axis=1`）。\n",
    "arr = np.arange(1,10).reshape(3,3)\n",
    "arr2 = np.insert(arr,1,[100,200,300],axis=1)\n",
    "print(arr2)\n",
    "# （5）创建数组 `arr = np.arange(1,11)`，删除索引 `[1,3,5]` 的元素；再删除当前数组中的所有偶数元素（使用布尔索引）。  \n",
    "print('*'*100)\n",
    "arr = np.arange(1,11)\n",
    "arr2 = np.delete(arr,[1,3,5])\n",
    "print(arr2)\n",
    "arr3 = np.delete(arr,arr%2==0)\n",
    "print(arr3)\n",
    "# （6）创建二维数组 `arr = np.arange(12).reshape(3,4)`，删除第 `0` 行与第 `2` 列（分别指定 `axis=0/1`）。  \n",
    "print('*'*100)\n",
    "arr = np.arange(12).reshape(3,4)\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "arr2 = np.delete(arr,0,axis=0)\n",
    "arr3 = np.delete(arr2,2,axis=1)\n",
    "print(arr3)\n",
    "\n",
    "# （7）给定三个数组  \n",
    "'''   \n",
    " a = `np.array([1,2])`，b = `np.array([3,4])`，c = `np.array([5,6])`：  \n",
    "    ① 使用 `np.concatenate` 合并为一维；  \n",
    "    ② 使用 `np.stack` 合并为二维（3×2）；  \n",
    "    ③ 分别用 `np.vstack` 与 `np.hstack` 展示纵向与横向拼接。\n",
    "'''\n",
    "print('*'*100)\n",
    "a = np.array([1,2])\n",
    "b = np.array([3,4])\n",
    "c = np.array([5,6])\n",
    "arr = np.concatenate((a,b,c))\n",
    "print(arr)\n",
    "arr2 = np.concatenate(([a],[b],[c]),axis=0)\n",
    "print(arr2)\n",
    "arr2 = np.vstack((a,b,c))\n",
    "arr3 = np.hstack((a.reshape(-1,1),b.reshape(-1,1),c.reshape(-1,1)))\n",
    "print(arr2)\n",
    "print(arr3)\n",
    "\n",
    "#（8）创建数组 `arr = np.array([10,20,30,40,50,60])`，用花式索引将索引 `[1,3,5]` 的元素同时改为 `-1`。  \n",
    "arr = np.array([10,20,30,40,50,60])\n",
    "arr[[1,3,5]] = -1\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "# （9）创建数组 `arr = np.array([10,20,30,40,50])`，将所有 `≥ 40` 的元素改为 `0`。  \n",
    "arr = np.array([10,20,30,40,50])\n",
    "arr[arr>=40] = 0\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "\n",
    "# （10）创建数组 `arr = np.arange(1,11)`，使用 `np.where` 将偶数改为 `0`，奇数保持原值。\n",
    "arr = np.arange(1,11)\n",
    "arr2 = np.where(arr%2==0,0,arr)\n",
    "print(arr2)\n",
    "print('*'*100)\n",
    "# （11）创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，使用 `np.put` 将线性位置 `0、5、10` 的元素改为 `99`（按一维顺序）。  \n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "#put在本体上面改\n",
    "np.put(arr,[0,5,10],99)\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "# （12）创建二维数组 `arr = np.arange(1,13).reshape(3,4)`，将第一行所有元素加 `10`；再将最后一列全部改为 `0`。  \n",
    "arr = np.arange(1,13).reshape(3,4)\n",
    "arr[0]+=10\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "arr[:,-1] = 0\n",
    "print(arr)\n",
    "# （13）创建数组 `arr = np.array([5,10,15,20,25,30])`，将 `> 20` 的元素改为 `-1`；将等于 `10` 的元素改为 `0`。  \n",
    "print('*'*100)\n",
    "arr = np.array([5,10,15,20,25,30])\n",
    "arr[arr>20] = -1\n",
    "arr[arr==10] = 0\n",
    "print(arr)\n",
    "\n",
    "''' \n",
    "（14）创建二维数组  \n",
    "      arr =  \n",
    "          [ [ 5,  2,  8,  1],  \n",
    "            [ 7,  9,  3,  4],  \n",
    "            [ 6,  0, 11, 12] ]  \n",
    "    依次完成：删除第 `2` 行；在末尾添加一行 `[100,101,102,103]`；将第 `0` 行的偶数改为 `0`；将第 `1` 列复制到末列（原列保留）；使用 `np.where` 将所有 `> 50` 的值改为 `-1`。  \n",
    "'''\n",
    "arr = np.array([[5,2,8,1],[7,9,3,4],[6,0,11,12]])\n",
    "print('*'*100)\n",
    "arr2 = np.delete(arr,1,axis=0)\n",
    "print(arr2)\n",
    "arr3 = np.append(arr2,[[100,101,102,103]],axis=0)\n",
    "print(arr3)\n",
    "arr3[0,arr3[0]%2==0] = 0\n",
    "print(arr3)\n",
    "print('*'*100)\n",
    "ta = arr3[:,1].reshape(-1,1)\n",
    "arr4 = np.hstack((arr3,ta))\n",
    "print(arr4)\n",
    "arr5 = np.where(arr4>50,-1,arr4)\n",
    "print('*'*100)\n",
    "print(arr5)\n",
    "#（15）创建数组 `arr = np.arange(1,21).reshape(4,5)`，依次完成：删除第 `1` 行；在第 `2` 列插入 `[99,99,99]`；将所有 `> 10` 的元素改为 `0`；将第 `0` 行的奇数改为 `-1`；使用 `np.put` 将线性位置 `0、5、10` 的值设为 `888`；输出最终结果。\n",
    "arr = np.arange(1,21).reshape(4,5)\n",
    "print('*'*100)\n",
    "arr2 = np.delete(arr,1,axis=0)\n",
    "print(arr2)\n",
    "arr3 = np.insert(arr2,2,[99,99,99],axis=1)\n",
    "print(arr3)\n",
    "arr3[arr3>10] = 0\n",
    "print(arr3)\n",
    "arr3[0,arr3[0]%2!=0] = -1\n",
    "print(arr3)\n",
    "np.put(arr3,[0,5,10],888)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6a0bf97-9189-4108-8af4-6b2fc0f10c1a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae7b8bf5-48a5-4a23-8482-f694cc0a6d84",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
